namespace GemBox.Spreadsheet
{
    using System;

    public sealed class ExcelRow : ExcelColumnRowBase
    {
        private ExcelCellCollection cellCollection;
        private CellRange cells;
        internal const int DefaultRowHeight = 0xff;
        private int height;

        internal ExcelRow(ExcelRowCollection parent, ExcelRow sourceRow) : base(parent, sourceRow)
        {
            this.height = 0xff;
            this.height = sourceRow.height;
            this.cellCollection = new ExcelCellCollection(parent.Parent, sourceRow.cellCollection);
        }

        internal ExcelRow(ExcelRowCollection parent, int index) : base(parent, index)
        {
            this.height = 0xff;
            this.cellCollection = new ExcelCellCollection(parent.Parent);
        }

        public void AutoFit()
        {
            int num = 1;
            CellRangeEnumerator readEnumerator = this.Cells.GetReadEnumerator();
            while (readEnumerator.MoveNext())
            {
                CellRange mergedRange = readEnumerator.CurrentCell.MergedRange;
                if ((mergedRange == null) || (mergedRange.Height <= 1))
                {
                    object obj2 = readEnumerator.CurrentCell.Value;
                    if (obj2 != null)
                    {
                        int num2 = Utilities.CountCharOccurenceInString((string) obj2, '\n') + 1;
                        if (num2 > num)
                        {
                            num = num2;
                        }
                    }
                }
            }
            this.height = num * 0xff;
        }

        public void Delete()
        {
            ((ExcelRowColumnCollectionBase) base.Parent).DeleteInternal(base.Index);
        }

        public void InsertCopy(int rowCount, ExcelRow sourceRow)
        {
            ((ExcelRowCollection) base.Parent).InsertInternal(base.Index, rowCount, sourceRow);
        }

        public void InsertEmpty(int rowCount)
        {
            ((ExcelRowCollection) base.Parent).InsertInternal(base.Index, rowCount, null);
        }

        public ExcelCellCollection AllocatedCells
        {
            get
            {
                return this.cellCollection;
            }
        }

        public CellRange Cells
        {
            get
            {
                if (this.cells == null)
                {
                    this.cells = new CellRange(base.Parent.Parent, base.Index, 0, base.Index, 0x3fff);
                }
                return this.cells;
            }
        }

        internal override int Extent
        {
            get
            {
                return this.Height;
            }
        }

        public int Height
        {
            get
            {
                return this.height;
            }
            set
            {
                this.height = value;
            }
        }
    }
}
